package com.starter.home.controller;


import com.starter.home.domain.entity.ServeInfo;
import com.starter.home.domain.entity.User;
import com.starter.home.domain.vo.ServeInfoVO;
import com.starter.home.domain.vo.ServeOrderVO;
import com.starter.home.service.IServeInfoService;
import com.starter.home.util.CommonParam;
import com.starter.home.util.PageUtil;
import com.starter.home.util.ResponseResult;
import com.starter.home.util.constant.UserConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2020-05-01
 */
@RestController
@RequestMapping("/info")
public class ServeInfoController {

    @Autowired
    private IServeInfoService infoService;

    @GetMapping(value = "/delete/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        infoService.deleteById(id);
        return ResponseResult.success(null);
    }
    /**
     * @return
     */
    @GetMapping(value = "/getList")
    public ResponseResult getList(@ModelAttribute CommonParam commonParam,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (!StringUtils.equals(UserConst.ADMIN,user.getAccount())){
            commonParam.setUserId(user.getId());
        }
        PageUtil pageUtil = infoService.getInfoList(commonParam);
        return ResponseResult.success(pageUtil);
    }

    @PostMapping(value = "/saveInfo")
    public ResponseResult saveInfo(@RequestBody ServeInfoVO serveInfoVO, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (Objects.isNull(user)){
            return ResponseResult.error(null);
        }
        ServeInfo serveInfo = new ServeInfo();
        BeanUtils.copyProperties(serveInfoVO,serveInfo);
        serveInfo.setCtype(serveInfoVO.getType());
        serveInfo.setUserId(user.getId());
        boolean save = infoService.save(serveInfo);
        if (save){
            return ResponseResult.success(null);
        }else {
            return ResponseResult.error(null);
        }
    }

    @GetMapping(value = "/getAll")
    public ResponseResult getAll(@ModelAttribute CommonParam commonParam,HttpSession session){
        PageUtil pageUtil = infoService.getInfoList(commonParam);
        return ResponseResult.success(pageUtil);
    }
    @GetMapping(value = "/getDetail/{id}")
    public ResponseResult getDetail(@PathVariable Integer id){
        ServeInfoVO infoDetail = infoService.getInfoDetail(id);
        return ResponseResult.success(infoDetail);
    }
    @GetMapping(value = "/getInfoLike")
    public ResponseResult getInfoLike(@ModelAttribute CommonParam params, HttpSession session){

        User user = (User) session.getAttribute("user");
        if (Objects.isNull(user)){
            return ResponseResult.error(null);
        }
        PageUtil list = infoService.getInfo(params,user.getId(),0);
        return ResponseResult.success(list);
    }
    @GetMapping(value = "/getInfoCart")
    public ResponseResult getInfoCart(@ModelAttribute CommonParam params,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (Objects.isNull(user)){
            return ResponseResult.error(null);
        }
        PageUtil list = infoService.getInfo(params, user.getId(),null);
        return ResponseResult.success(list);
    }


}
